package cn.zhoutingrui.pethome.system.shop.controller;

import cn.zhoutingrui.pethome.base.constants.BaseConstants;
import cn.zhoutingrui.pethome.system.shop.service.IShopAuditLogService;
import cn.zhoutingrui.pethome.system.shop.domain.ShopAuditLog;
import cn.zhoutingrui.pethome.system.shop.query.ShopAuditLogQuery;
import cn.zhoutingrui.pethome.utils.AjaxResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shopAuditLog")
public class ShopAuditLogController {
    @Autowired
    public IShopAuditLogService shopAuditLogService;


    /**
     * 保存和修改公用的
     * @param shopAuditLog  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResultUtil addOrUpdate(@RequestBody ShopAuditLog shopAuditLog){
        try {
            if( shopAuditLog.getId()!=null)
                shopAuditLogService.update(shopAuditLog);
            else
                shopAuditLogService.add(shopAuditLog);
            return AjaxResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail().setMsg(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResultUtil delete(@PathVariable("id") Long id){
        try {
            shopAuditLogService.delete(id);
            return AjaxResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail().setMsg(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public ShopAuditLog get(@PathVariable("id")Long id)
    {
        return shopAuditLogService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<ShopAuditLog> list(){
        return shopAuditLogService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResultUtil json(@RequestBody ShopAuditLogQuery query)
    {
        try {
            List<ShopAuditLog> list = shopAuditLogService.findQuery(query);
            Long count = shopAuditLogService.findByQueryCount(query);
            AjaxResultUtil ajaxResultUtil = AjaxResultUtil.success().setData(list).setTotal(count);
            return ajaxResultUtil;
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail();
        }
    }

    @PostMapping("/audit/pass")
    @ApiOperation("审核通过")
    public AjaxResultUtil pass(@RequestBody ShopAuditLog shopAuditLog){
        shopAuditLog.setState(BaseConstants.Shop.STATE_WAIT_ACTIVE);
        shopAuditLogService.add(shopAuditLog);
        return AjaxResultUtil.success();
    }

    @PostMapping("/audit/reject")
    @ApiOperation("审核驳回")
    public AjaxResultUtil reiect(@RequestBody ShopAuditLog shopAuditLog){
        shopAuditLog.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
        shopAuditLogService.add(shopAuditLog);
        return AjaxResultUtil.success();
    }
}
